//-----------------------------------------------------------------------
// <copyright file="tblInternalRateDAO.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Robot.Entities;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Robot.Data
{
  public class tblInternalRateDAO : tblInternalRateDAOBase
  {
    public tblInternalRateDAO()
    {
    }

    public virtual tblInternalRateCollection GettblInternalRateByFromFileID(Guid id)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblInternalRateGetByFileID");

        database.AddInParameter(dbCommand, "@FileID", DbType.Guid, id);

        tblInternalRateCollection tblInternalRateCollection = new tblInternalRateCollection();
        using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
        {
          while (reader.Read())
          {
            tblInternalRate tblInternalRate = CreatetblInternalRateFromReader(reader);
            tblInternalRateCollection.Add(tblInternalRate);
          }
          reader.Close();
        }
        return tblInternalRateCollection;
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessGettblInternalRateException, ex);
      }
    }

    public virtual tblInternalRateCollection GettblInternalRateOnDay()
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblInternalRateGetOnDay");

        tblInternalRateCollection tblInternalRateCollection = new tblInternalRateCollection();
        using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
        {
          while (reader.Read())
          {
            tblInternalRate tblInternalRate = CreatetblInternalRateFromReader(reader);
            tblInternalRateCollection.Add(tblInternalRate);
          }
          reader.Close();
        }
        return tblInternalRateCollection;
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessGettblInternalRateException, ex);
      }
    }

    public virtual tblInternalRate GetInternalRateByCurrency(string buyCCY, string sellCCY)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblInternalRateGetByCurrency");

        database.AddInParameter(dbCommand, "@BuyCCY", DbType.AnsiString, buyCCY);
        database.AddInParameter(dbCommand, "@SellCCY", DbType.AnsiString, sellCCY);
       // database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);

        tblInternalRate tblInternalRate = null;
        using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
        {
          if (reader.Read())
          {
            tblInternalRate = CreatetblInternalRateFromReader(reader);
            reader.Close();
          }
        }
        return tblInternalRate;
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessGettblInternalRateException, ex);
      }
    }


    #region DeletetblInternalRate methods

    public virtual void DeletetblInternalRate(string peoplewise)
    {
      try
      {
        Database database = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = database.GetStoredProcCommand("_sptblInternalRateDeleteAllOnDay");

        database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);

        DataAccessHelper.ExecuteNonQuery(database, dbCommand);
      }
      catch (Exception ex)
      {
        // log this exception
        Logger.Error(ex);
        // wrap it and rethrow
        throw new DataAccessException(SR.DataAccessDeletetblInternalRateException, ex);
      }
    }

    #endregion

  }
}
